package com.fafu.control;

import com.fafu.dao.mapper.OtherquestionMapper;
import com.fafu.model.Message;
import com.fafu.pojo.MessageAndPics;
import com.fafu.pojo.NoReadPojoAndWorkerList;
import com.fafu.service.OtherService;
import com.fafu.service.WorkerService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;
import util.Dictionary;
import util.Dictionary.CODE;
import util.JsonRsp;
import util.MyMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static util.Dictionary.CODE.SUCCESS;
import static util.Dictionary.CODE.SYSTEM_ERROR;
import static util.Dictionary.CODE.TYPE_ERROR;

@Api("其他问题")
@RestController
@RequestMapping("/other")
public class OtherControl {
    private Logger logger = LoggerFactory.getLogger(OtherControl.class);
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private WorkerService workerService;
    @Autowired
    private OtherService otherService;

    @ApiOperation("新增公共报修")
    @RequestMapping(value = "/repair",method = RequestMethod.POST)
    public JsonRsp studentRepair(
            @RequestBody @ApiParam("报修的信息") MessageAndPics message
            ,@RequestParam(value = "isAlert", defaultValue = "1") @ApiParam(value = "是否广播")Integer isAlert){
        try{
            message.setState(Dictionary.REPAIR.REPAIR_STATE_NO_READ);
            message.setAddTime(System.currentTimeMillis());
            Map<String,Object> remap = otherService.addMessage(new MyMap().put("data",message).toMap());
            if(isAlert == 1)
                messagingTemplate.convertAndSend("/demo_2/topic/getResponse","search");
            return JsonRsp.success(new MyMap().put("id",remap.get("data")).toMap());
        }catch (Exception e){
            logger.error("系统内部错误"+e);
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }

    }
    @ApiOperation("查询公共报修Item")
    @RequestMapping(value = "/searchOtherItem",method = RequestMethod.GET)
    public JsonRsp searchOtherItem(
            @RequestParam(value="id") @ApiParam("id") Integer id){
        try{
            Map<String,Object> reMap = otherService.searchItemAndPics(new MyMap().put("id",id).toMap());
            Integer code = (Integer) reMap.get("code");
            if(code.equals(SUCCESS)) {
                NoReadPojoAndWorkerList redata = new NoReadPojoAndWorkerList();
                List relist = workerService.searchWorkerList(new MyMap().put("key","").toMap());
                redata.setWorkerList(relist);
                redata.setData(reMap.get("data"));
                return JsonRsp.success(redata);
            }
            return JsonRsp.fail(code, (String) reMap.get("msg"));
        }catch (Exception e){
            logger.error("系统内部错误"+e);
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }

    }
    @ApiOperation("拒绝维修")
    @RequestMapping(value = "/refuseOtherItem",method = RequestMethod.GET)
    public JsonRsp refuseOtherItem(
            @RequestParam(value="id") @ApiParam("维修id") Integer id
            ,@RequestParam(value = "operationId")@ApiParam("管理员id") Integer operationId
            ,@RequestParam("msg")@ApiParam("拒绝维修的信息")String msg){
        try{
            Map<String,Object>reMap = otherService.refuse(new MyMap()
                    .put("id",id)
                    .put("operationId",operationId)
                    .put("msg",msg).toMap());
            if(reMap.get("code").equals(CODE.SUCCESS))
                return JsonRsp.success("success");
            return JsonRsp.fail((Integer) reMap.get("code"),(String)reMap.get("msg"));
        }catch (Exception e){
            logger.error("系统内部错误"+e);
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("管理员提交报修")
    @ResponseBody
    @RequestMapping(value = "/managerRepair",method=RequestMethod.POST)
    public JsonRsp managerRepair(
            @ApiParam(value = "id")@RequestParam(value = "id")Integer id
            ,@ApiParam(value="操作员id")@RequestParam(value = "operationId") Integer operationId
            ,@ApiParam(value="师傅id")@RequestParam(value = "workerId") Integer workerId){
        try{
            Map<String,Object>reMap = otherService.managerRepair(new MyMap()
                    .put("id",id)
                    .put("operationId",operationId)
                    .put("workerId",workerId).toMap());
            if(reMap.get("code").equals(CODE.SUCCESS))
                return JsonRsp.success("success");
            return JsonRsp.fail((Integer) reMap.get("code"),(String)reMap.get("msg"));
        }catch(Exception e){
            logger.error(""+e);
            return JsonRsp.fail(SYSTEM_ERROR,"system error");
        }
    }

    @ApiOperation("管理员查询进行时或历史的公共报修")
    @RequestMapping(value = "/repairROHOthereItem",method = RequestMethod.GET)
    public JsonRsp repairROHOthereItem(
            @ApiParam("itemId")@RequestParam("itemId")Integer itemId) {
        try {
            Map<String,Object> reMap = otherService.searchROHItem(itemId);
            if(reMap.get("code").equals(SUCCESS))
                return JsonRsp.success(reMap.get("data"));
            return JsonRsp.fail((Integer)reMap.get("code"),(String)reMap.get("msg"));
        } catch (Exception e) {
            logger.error("内部异常" + e);
            return JsonRsp.fail(SYSTEM_ERROR, "系统内部异常");
        }
    }

    @ApiOperation("管理员查询拒绝的公共list")
    @RequestMapping(value = "/refuseList",method = RequestMethod.GET)
    public JsonRsp refuseList(
            @ApiParam("operationId")@RequestParam("operationId")Integer operationId
            ,@ApiParam("type:默认0：全部，1：自己")@RequestParam(value = "type",defaultValue = "0",required = false)Integer type
            ,@ApiParam("第几页")@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum
            ,@ApiParam("每页几条，默认20")@RequestParam(value = "pageSize",defaultValue = "20")Integer pageSize) {
        try{
            MyMap paramMap = new MyMap().put("operationId",operationId);
            if(type == 1)
                paramMap.put("type",type);
            Map<String,Object> reMap= otherService.searchRefuseList(paramMap.put("pageNum",pageNum).put("pageSize",pageSize).toMap());

            return JsonRsp.success(reMap.get("data")).setCount((Long) reMap.get("count"));
        }catch (Exception e){
            logger.info(""+e);
            return JsonRsp.fail(SYSTEM_ERROR,"system error");
        }
    }
}
